#!/bin/bash
# OpenMPTCProuter VPS service script

_multipath() {
	# Force multipath status
	source /etc/shorewall/params.net
	for intf in `ls -1 /sys/class/net`; do
		if [ "$intf" != "bonding_masters" ]; then
			if [ "$intf" = "$NET_IFACE" ]; then
				[ "$(multipath $intf | tr -d '\n')" != "$intf is in default mode" ] && multipath $intf on
			else
				[ "$(multipath $intf | tr -d '\n')" != "$intf is deactivated" ] && multipath $intf off
			fi
		fi
	done
}

_glorytun_udp() {
	[ -z "$(glorytun show dev gt-udp-tun0 2>/dev/null | grep tunnel)" ] && {
		logger -t "OMR-Service" "Restart Glorytun-UDP"
		systemctl -q restart 'glorytun-udp@*'
	}
	for intf in /etc/glorytun-udp/tun*; do
		[ "$(echo $intf | grep key)" = "" ] && /etc/glorytun-udp/post.sh ${intf}
	done
}

_glorytun_tcp() {
	for intf in /etc/glorytun-tcp/tun*; do
		[ "$(echo $intf | grep key)" = "" ] && /etc/glorytun-tcp/post.sh ${intf}
	done
	if [ -f /etc/openmptcprouter-vps-admin/current-vpn ] && [ "$(cat /etc/openmptcprouter-vps-admin/current-vpn)" = "glorytun_tcp" ]; then
		if [ "$(ping -c 5 -w 5 10.255.255.2 | grep '100%')" != "" ] && [ "$(expr $(date +%s) - $(stat -c %Y /proc/$(pgrep glorytun-tcp)/exe ))" -gt "300" ]; then
			logger -t "OMR-Service" "No answer from VPN client end, restart Glorytun-TCP"
			systemctl restart glorytun-tcp@tun0
		fi
	fi
}

_omr_api() {
	[ -z "$(curl -s -k -m 30 https://127.0.0.1:65500/)" ] && {
		logger -t "OMR-Service" "Restart OMR-Admin"
		systemctl -q restart omr-admin
	}
}

_lan_route() {
	cat /etc/openmptcprouter-vps-admin/omr-admin-config.json | jq -c '.users[0][]' |
	while IFS=$"\n" read -r c; do
		vpnremoteip=$(echo "$c" | jq -r '.vpnremoteip')
		if [ -n "$vpnremoteip" ] && [ "$vpnremoteip" != "null" ]; then
			echo "$c" | jq -c '.lanips //empty' | 
			while IFS=$"\n" read -r d; do
				network=$(ipcalc -n $d | grep Network | awk '{print $2}')
				[ -n "$network" ] && [ -z "$(ip r show $network via $vpnremoteip)" ] && ip r replace $network via $vpnremoteip 2>&1 >/dev/null
			done
		fi
	done
}

_gre_tunnels() {
	. "$(readlink -f "/etc/shorewall/params.vpn")"
	for intf in /etc/openmptcprouter-vps-admin/intf/*; do
		if [ -f "$intf" ]; then
			. "$(readlink -f "$intf")"
			iface="$(basename $intf)"
			if [ "$(ip tunnel show $iface 2>/dev/null | awk '{print $4}')" != "$OMR_ADDR" ]; then
				ip tunnel del $iface 2>&1 >/dev/null
				ip tunnel add $iface mode gre local $INTFADDR remote $OMR_ADDR
				ip link set $iface up
				ip addr add $LOCALIP dev $iface
				ip route add $NETWORK dev $iface 2>&1 >/dev/null
			fi
		fi
	done
}

_openvpn_bonding() {
	if [ "$(ip link show ovpnbonding1)" != "" ] && ([ "$(ip link show ovpnbonding1 | grep SLAVE)" = "" ] || [ "$(ip link show omr-bonding | grep DOWN)" != "" ] || [ "$(ip link show | grep ovpnbonding | grep -c SLAVE | tr -d '\n')" != "8" ]); then
		echo 0 > /sys/class/net/omr-bonding/bonding/mode
		ip link set ovpnbonding1 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding1 up
		ip link set ovpnbonding2 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding2 up
		ip link set ovpnbonding3 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding3 up
		ip link set ovpnbonding4 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding4 up
		ip link set ovpnbonding5 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding5 up
		ip link set ovpnbonding6 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding6 up
		ip link set ovpnbonding7 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding7 up
		ip link set ovpnbonding8 master omr-bonding 2>&1 >/dev/null
		ip link set ovpnbonding8 up
		ip link set omr-bonding up mtu 1440 2>&1 >/dev/null
		ip a add 10.255.248.1 dev omr-bonding 2>&1 >/dev/null
		ip r add 10.255.248.0/24 dev omr-bonding 2>&1 >/dev/null
		ip r add 10.255.248.2 dev omr-bonding src 10.255.248.1 2>&1 >/dev/null
	fi
}

modprobe bonding 2>&1 >/dev/null
ip link add omr-bonding type bond 2>&1 >/dev/null
while true; do
	_glorytun_udp
	_glorytun_tcp
	_multipath
	_omr_api
	_lan_route
	_gre_tunnels
	_openvpn_bonding
	sleep 10
done
